package com.lswms.job;

import com.lswms.common.redis.RedisUtils;
import com.lswms.common.utils.MoneyUtil;
import com.lswms.common.utils.ZqDateUtil;
import com.lswms.dto.YnLotteryDTO;
import com.lswms.dto.YnSignDTO;
import com.lswms.dto.vo.resp.OrderDataListResp;
import com.lswms.entity.YnLotteryEntity;
import com.lswms.entity.YnSignEntity;
import com.lswms.service.YnLotteryService;
import com.lswms.service.YnSignService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


/**
 * 
 * @author Administrator
 *
 */
@Component
@Slf4j
public class RobotBigDataTasks {

    @Autowired
    private RedisUtils redisUtils;
	
    /**
     * 大数据（1秒一次）
     */
    //@Scheduled(cron="*/1 * * * * ?")
    public void setRobotls(){
    	log.info("开始大数据***");
        List<String> ls = new ArrayList<String>();
        ls.add("86628903");
        ls.add("76930003");
        
        int secord = 0;
        BigDecimal secOrder = getRatePf();
        if(secOrder.compareTo(BigDecimal.ZERO) <= 1){
        	secord = 1;
        }else{
        	secord = Integer.parseInt(MoneyUtil.bigDecimal2String(secOrder, 0));
        }
        //缓存字符串
        String caStr = "";
		Calendar calendar = Calendar.getInstance();
		int curHour24 = calendar.get(calendar.SECOND);
		if(curHour24%secord == 0){
        	int num = (int) (Math.random() * 2);
        	String pre = ls.get(num);
        	int hre = (int)((Math.random()*9+1)*1000000);
        	int amount = getRandom();
        	String mes = ZqDateUtil.yyyyMMddHHmmssSSSHl()+","+amount+","+pre+""+hre+","+ZqDateUtil.yyyyMMddHHmm(ZqDateUtil.getNow());
        	if(StringUtils.isBlank(caStr)){
        		caStr = mes;
        	}else{
        		caStr = mes+"#"+caStr;
        	}
        	//当天今天和订单数
        	String ymd = ZqDateUtil.yyyyMMdd(ZqDateUtil.getNow());
        	String todayAmount = (String)redisUtils.hGet("todayAmount:"+ymd, ymd);
        	int totalAmount = amount;
        	if(!StringUtils.isBlank(todayAmount)){
        		totalAmount = amount + Integer.parseInt(todayAmount);
        	}
        	redisUtils.hSet("todayAmount:"+ymd,ymd,totalAmount+"");
        	
        	String todayOrder = (String)redisUtils.hGet("todayOrder:"+ymd, ymd);
        	int totalOrder = 1;
        	if(!StringUtils.isBlank(todayOrder)){
        		totalOrder = 1 + Integer.parseInt(todayOrder);
        	}
        	redisUtils.hSet("todayOrder:"+ymd,ymd,totalOrder+"");
		}
        String strOrder = (String)redisUtils.hGet("allOderList:", "order");
        if(StringUtils.isBlank(strOrder)){
        	strOrder = caStr;
        }else{
        	strOrder = caStr+"#"+strOrder;
        }
        String saveMes = "";
        for(int i = 0;i<strOrder.split("#").length;i++){
        	String ord = strOrder.split("#")[i];
        	if(!StringUtils.isBlank(ord)){
        		if(StringUtils.isBlank(saveMes)){
        			saveMes = ord;
        		}else{
        			saveMes = saveMes+"#"+ord;        			
        		}
        	}
        	if(i>= 5){
        		break;
        	}
        }
        redisUtils.hSet("allOderList:", "order",saveMes);
    	log.info("结束大数据***");
    }
    //获取当前小时
    public int getHour(){
		Calendar calendar = Calendar.getInstance();
		int curHour24 = calendar.get(calendar.HOUR_OF_DAY);
		return curHour24;
    }
    //获取范围内随机数
    public int getRandom(){
        int a = ThreadLocalRandom.current().nextInt(0, 3);
    	List<Integer> list = new ArrayList<Integer>();
    	list.add(1);
    	list.add(2);
    	list.add(5);
    	list.add(10);
        return list.get(a);
    }
    //获取时间段比例平分
    public BigDecimal getRatePf(){
    	String ymd = ZqDateUtil.yyyyMMdd(ZqDateUtil.getNow());
    	String order = (String)redisUtils.hGet("Order:"+ymd, ymd);
    	BigDecimal totalOrder = MoneyUtil.convertBigDecimal(order);
    	int hour = getHour();
    	if(hour >= 0 && hour <= 11){
    		BigDecimal ntotal = totalOrder.multiply(new BigDecimal(0.05));
    		BigDecimal sec = ntotal.divide(new BigDecimal(12).multiply(new BigDecimal(60)), 2, BigDecimal.ROUND_HALF_UP);
			BigDecimal reva = new BigDecimal(60).divide(sec,2, BigDecimal.ROUND_HALF_UP);
			return reva;
    	}else if(hour > 11 && hour <= 14){
    		BigDecimal ntotal = totalOrder.multiply(new BigDecimal(0.2));
    		BigDecimal sec = ntotal.divide(new BigDecimal(3).multiply(new BigDecimal(60)), 2, BigDecimal.ROUND_HALF_UP);
			BigDecimal reva = new BigDecimal(60).divide(sec,2, BigDecimal.ROUND_HALF_UP);
			return reva;
    	}else if(hour > 14 && hour <= 17){
    		BigDecimal ntotal = totalOrder.multiply(new BigDecimal(0.1));
    		BigDecimal sec = ntotal.divide(new BigDecimal(3).multiply(new BigDecimal(60)), 2, BigDecimal.ROUND_HALF_UP);
			BigDecimal reva = new BigDecimal(60).divide(sec,2, BigDecimal.ROUND_HALF_UP);
			return reva;
    	}else if(hour > 17 && hour <= 20){
    		BigDecimal ntotal = totalOrder.multiply(new BigDecimal(0.35));
    		BigDecimal sec = ntotal.divide(new BigDecimal(3).multiply(new BigDecimal(60)), 2, BigDecimal.ROUND_HALF_UP);
			BigDecimal reva = new BigDecimal(60).divide(sec,2, BigDecimal.ROUND_HALF_UP);
			return reva;
    	}else if(hour > 20 && hour <= 24){
    		BigDecimal ntotal = totalOrder.multiply(new BigDecimal(0.3));
    		BigDecimal sec = ntotal.divide(new BigDecimal(4).multiply(new BigDecimal(60)), 2, BigDecimal.ROUND_HALF_UP);
			BigDecimal reva = new BigDecimal(60).divide(sec,2, BigDecimal.ROUND_HALF_UP);
			return reva;
    	}
    	return BigDecimal.ZERO;
    }
    //获取范围内随机数(百分比)
    public BigDecimal getRandomRate(int start,int end){
        int a = ThreadLocalRandom.current().nextInt(start, end);
        BigDecimal rate = new BigDecimal(a).divide(new BigDecimal(100));
        return rate;
    }

    /**
     * 大数据（1秒一次）
     */
    @Scheduled(cron="0 5 * * * ? ")
    public void setRobotlRate(){
    	log.info("开始大数据***");
    	String ymd = ZqDateUtil.yyyyMMdd(ZqDateUtil.getNow());
    	
    	String onlineRateR1 = (String)redisUtils.hGet("onlineRate:"+ymd, ymd);
    	if(!StringUtils.isBlank(onlineRateR1)){
    		BigDecimal r1 = new BigDecimal(onlineRateR1);
    		
            int a = ThreadLocalRandom.current().nextInt(0, 1);
            int b = ThreadLocalRandom.current().nextInt(1, 2);
            BigDecimal c = new BigDecimal(b).multiply(new BigDecimal(0.1));
            if(a == 0){
            	r1 = r1.subtract(c);
            }else {
            	r1 = r1.add(c);
            }
            redisUtils.hSet("onlineRateFD:"+ymd,ymd, MoneyUtil.bigDecimal2String(r1, 2));
    	}
    	log.info("结束大数据***");
    }

}
